package demo1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-09-08
 * Time: 11:56
 */
public class Test {

    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list1 = new ArrayList<>();
        int n = nums.length;
        Arrays.sort(nums);
        for(int i = 0; i < n - 2; i++){
            int left = 0, right = n - 2 - i;
            while(left < right){
                if(nums[left] + nums[right] > - nums[n - i - 1]){
                    right--;
                }else if(nums[left] + nums[right] < - nums[n - i - 1]){
                    left++;
                }else{
                    List<Integer> list2 = new ArrayList<>();
                    list2.add(nums[left]);
                    list2.add(nums[right]);
                    list2.add(nums[n - i - 1]);
                    list1.add(list2);
                    int n1 = nums[right];
                    int n2 = nums[left];
                    while(left < n && nums[left] == n2){
                        left++;
                    }
                    while(right > 0 && nums[right] == n1){
                        right--;
                    }

                }
            }
            while(i < n - 2 && nums[n - 1 - i] == nums[n - 2 - i]){
                i++;
            }
        }
        return list1;
    }

    public static void main(String[] args) {
        int[] arr = {0,0,0,0};
        //threeSum(arr);
    }
}
